package com.cn.studey.service.impl;

import com.cn.studey.common.Constant;
import com.cn.studey.service.RedisService;
import com.cn.studey.service.TokenService;
import com.cn.studey.utils.JwtTokeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author mjy
 * @date 2021/12/29
 * @description
 */
@Service
public class TokenServiceImpl implements TokenService {

    @Autowired
    private RedisService redisService;

    @Override
    public String createToken(Long id) {
        if (id == null) {
            return null;
        }
        //获取签名token
        String token = JwtTokeUtil.sign("userId", String.valueOf(id), Constant.TOKEN_OVERDUE);
        //用户id为值,以token为值,存储redis
        redisService.setKey(String.valueOf(id), token, Constant.TOKEN_OVERDUE, TimeUnit.SECONDS);
        return token;
    }
}
